package com.limingru.online_class.service.impl;

import com.limingru.online_class.common.ServiceResultEnum;
import com.limingru.online_class.controller.param.LoginUserInfo;
import com.limingru.online_class.domain.User;
import com.limingru.online_class.domain.vo.UserVO;
import com.limingru.online_class.mapper.UserMapper;
import com.limingru.online_class.service.UserService;
import com.limingru.online_class.utils.AvatarUtil;
import com.limingru.online_class.utils.JWTUtil;
import com.limingru.online_class.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public String save(Map<String, String> userInfoParam) {
        User user = parseToUser(userInfoParam);
        if (user != null) {
            if (checkUserExist(user)) {
                return ServiceResultEnum.USER_EXIST.getText();
            }
            return userMapper.saveUser(user) > 0 ? ServiceResultEnum.SUCCESS.getText() : ServiceResultEnum.FAIL.getText();
        }
        return ServiceResultEnum.PHONE_OR_PWD_NOT_EMPTY.getText();
    }

    @Override
    public String findUser(LoginUserInfo loginUserInfo) {
        String phone = loginUserInfo.getPhone();
        String pwd = loginUserInfo.getPwd();
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(pwd)) {
            return ServiceResultEnum.PHONE_OR_PWD_NOT_EMPTY.getText();
        }
        User user = new User();
        user.setPhone(phone);
        user.setPwd(MD5Util.MD5Encode(pwd));
        List<User> userList = userMapper.findUserByPhoneAndPwd(user);
        if (userList.size() > 0) {
            User resUser = userList.get(0);
            return JWTUtil.generateJWT(resUser);
        } else {
            return ServiceResultEnum.PHONE_OR_PWD_ERROR.getText();
        }
    }

    @Override
    public UserVO listUserByIdAndName(Integer id, String name) {
        UserVO userVO = new UserVO();
        User user = new User();
        user.setId(id);
        user.setName(name);
        List<User> users = userMapper.findUserByIdAndName(user);
        if (users.size() > 0) {
            BeanUtils.copyProperties(users.get(0), userVO);
            return userVO;
        }
        return null;
    }

    private User parseToUser(Map<String, String> userInfoParam) {
        if (userInfoParam.containsKey("name") && userInfoParam.containsKey("phone") && userInfoParam.containsKey("pwd")) {
            String phone = userInfoParam.get("phone");
            String pwd = userInfoParam.get("pwd");
            String name = userInfoParam.get("name");
            if (!(StringUtils.isEmpty(phone) || StringUtils.isEmpty(pwd) || StringUtils.isEmpty(name))) {
                User user = new User();
                user.setName(name);
                user.setPhone(phone);
                user.setPwd(MD5Util.MD5Encode(pwd));
                user.setCreateTime(new Date());
                user.setHeadImg(AvatarUtil.getRandomAvatar());
                return user;
            }
            return null;
        } else {
            return null;
        }
    }

    private boolean checkUserExist(User user) {
        List<User> result = userMapper.findUserByPhone(user);
        return result.size() > 0;
    }
}

